package com.spring.common.web.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.spring.common.web.dao.CommonOaMapper;
import com.spring.common.web.model.CommonOa;
import com.spring.common.web.service.CommonOaService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.tools.Result;

@Service
public class CommonOaServiceImpl implements CommonOaService{
	@Resource
	private CommonOaMapper commonOaMapper;

	@Override
	public Result auditedByTableName(CommonOa oa) {
		Result ret = new Result();
		Map<String,String> parm = Maps.newHashMap();
		parm.put("tableName", oa.getTablename());
		parm.put("propertyName", "EntityAvailableState");
		parm.put("stateName", oa.getStateName());
		parm.put("stateValue", oa.getStatus());
		parm.put("remark", oa.getStateName());
		parm.put("operatorId", oa.getUser_id());
		parm.put("operatorName", oa.getUser_name());
		String[] ids = oa.getId().split(",");
		if(ids.length>0){
			for(String id:ids){
				oa.setId(id);
				parm.put("id", oa.getId());
				commonOaMapper.auditedByTableName(oa);
				commonOaMapper.insertWorkFlowLog(parm);
				updateInfoByType(oa);
			}
		}
		return ret;
	}
	
	//同步更新数据
	private void updateInfoByType(CommonOa oa){
		if(ApplicationUtils.isNotNull(oa.getType())){
			if("rk".equalsIgnoreCase(oa.getType())){//入库
				List<Map<String,Object>> rkList = commonOaMapper.queryProRkNum(oa);
				if(rkList!=null){
					for(Map<String,Object> m:rkList){
//						String order_productId = ApplicationUtils.Object2String(m.get("order_productId"));
//						int inQty = Integer.parseInt(ApplicationUtils.Object2String(m.get("inQty")));
						commonOaMapper.updateOrderProKidParkRk(m);
					}
				}
			}
			if("ck".equalsIgnoreCase(oa.getType())){//出库
				List<Map<String,Object>> ckList = commonOaMapper.queryProCkNum(oa);
				if(ckList!=null){
					for(Map<String,Object> m:ckList){
//						String order_productId = ApplicationUtils.Object2String(m.get("order_productId"));
//						int outQty = Integer.parseInt(ApplicationUtils.Object2String(m.get("outQty")));
						commonOaMapper.updateOrderProKidParkCk(m);
					}
				}
			}
		}
	}
}
